package top.zackyoung.bbs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.zackyoung.bbs.entity.User;
import top.zackyoung.bbs.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: ZackYoung
 * @time: 2020/6/5
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView loginPage(ModelAndView mav, String url, String target) {
        mav.setViewName("login_test");
        mav.addObject("url", url);
        mav.addObject("target", target);
        return mav;
    }

//    @PostMapping("/login")
//    public ModelAndView loginDo(ModelAndView mav, String user, String pw, String url, HttpServletRequest req) {
//        try {
//            User user1 = userService.login(user, pw);
//            if (user1 != null) {
//                if (url == null) {
//                    mav.setViewName("redirect:/index");
//                } else {
//                    mav.setViewName("redirect:" + url);
//                }
//                req.getSession().setAttribute("userList", user1);
//            } else {
//                String msg = "用户名或密码错误";
//                mav.addObject("msg", msg);
//                mav.setViewName("login");
//            }
//
//        } catch (RuntimeException e) {
//            e.printStackTrace();
//        } finally {
//            return mav;
//        }
//    }

    @PostMapping("/login")
    @ResponseBody
    public Map loginDo(HttpServletRequest req, String user, String pw) {
        Map map = new HashMap();
        Map error = new HashMap();
        try {
            User user1 = userService.login(user, pw);
            req.getSession().setAttribute("userList", user1);

            map.put("success", "true");
        } catch (RuntimeException e) {
            e.printStackTrace();
            String msg = "用户名或密码错误";
            map.put("success", "false");
            error.put("errorMsg", msg);
            map.put("error", error);
        } finally {
            return map;
        }
    }

    @PostMapping("/register")
    @ResponseBody
    public Map registerDo(HttpServletRequest req, String user, String pw, String nikename, String email, String QQ) {
        Map map = new HashMap();
        Map error = new HashMap();
        User user1 = userService.queryUserInformation(user);
        if (user.equals("")) {
            error.put("userError", "请输入用户名");
        }
        if (pw.length() < 6) {
            error.put("pwError", "密码长度必须大于6");
        }
        if (pw.equals("")) {
            error.put("pwError", "请输入密码");
        }
        if (nikename.equals("")) {
            error.put("nikeError", "请输入昵称");
        }
        if (user1 != null) {
            error.put("userError", "用户名已被注册");
        }

        if (!error.isEmpty()) {
            map.put("success", "false");
            map.put("error", error);
        } else {
            User user2 = new User();
            user2.setUsername(user);
            user2.setPassword(pw);
            user2.setNikename(nikename);
            user2.setEmail(email);
            user2.setQq(QQ);
            int result = userService.addUser(user2);
            if (result>0){
                map.put("success", "true");
            }
        }
        return map;
    }

    @PostMapping("/logout")
    public ModelAndView logoutDo(ModelAndView mav, HttpServletRequest req) {
        req.getSession().invalidate();
        mav.setViewName("redirect:/index");
        return mav;
    }

//    @RequestMapping("login_test")
//    public ModelAndView login_test(ModelAndView mav) {
//        mav.setViewName("login_test");
//        return mav;
//    }

}
